package wiiusej.activeTaskManagement;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import wiiusej.Wiimote;
import wiiusej.apptest.WiimoteEventListener;

public class PermanenSoundController extends PermanentController{

	/*
	private final WiimoteEventListener myListener;
	private final Wiimote myWiimote;
	private final SoundShareCell soundCell;
	private final Lock lock;
	private final Condition condition;
	private final Logger fileLogger;
	
	private int freqMillis;
	private static final int INITIAL_FREQ = 2000;
	private static final int MAX_FREQ = 500;*/
	
	/*
	public PermanenSoundController(WiimoteEventListener myListener, Wiimote wiimote, 
			SoundShareCell soundCell, Lock lock, Condition condition){
	
		this.myListener = myListener;
		this.myWiimote = wiimote;
		this.soundCell = soundCell;
		this.lock = lock;
		this.condition = condition;
		
		PropertyConfigurator.configure("log4j.properties");
		this.fileLogger = Logger.getLogger("speaker");
	}*/
	
	
	//Speaker Parameters
	private static final int FIRST_LEVEL_FREQ = 2000;
	private static final int SECOND_LEVEL_FREQ = 1500;
	private static final int THIRD_LEVEL_FREQ = 1000;
	private static final int FOURTH_LEVEL_FREQ = 500;
	private static final int FIFTH_LEVEL_FREQ = 250;
	private static final int SIXTH_LEVEL_FREQ = 125;
	
	
	//queue
	private BlockingQueue<Integer> fromRealTime;
	
	//
	private int currentFreq = 1250;
	private final int slowFreq = 1000;
	
	public PermanenSoundController(WiimoteEventListener myListener, Wiimote wiimote, 
			SharedCell sharedCell, Lock lock, Condition condition, BlockingQueue<Integer> fromRealTime){		
		super(myListener, wiimote, sharedCell, lock, condition);
		
		this.freqMillis = 1250;
		
		this.fromRealTime = fromRealTime;
	}
	
	
	@Override
	public void run() {

		/*
		//wait until lock is released
		super.lock.lock();		
		
		try{			
			while(!myListener.isSpeakerStatus()){
				//fileLogger.debug("Waiting for SPEAKER CONDITION");
				condition.await();				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			freqMillis = super.sharedCell.getFrequencyMillis();			
			//fileLogger.debug("Releasing the SPEAKER LOCK. SIGNALED --> SpeakerStatus: "
				//	+ myListener.isSpeakerStatus());
			super.lock.unlock();			
		}		*/
		
		while(true){			
			
			try {
				
				Integer freq = fromRealTime.poll(slowFreq, TimeUnit.MILLISECONDS);
				
				if(freq != null){
					makeSound(freq);					
				}else{
					myWiimote.sendSound();
				}
				
				
				
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
						
		}
	}
	
	private void makeSound(Integer freq) {
		
		//super.uploadSoundFrequency();
		
		int i;
		
		switch(freq){
		
		case FIFTH_LEVEL_FREQ:
			
			for(i = 0; i < 2; i++){
				
				myWiimote.sendSound();
				
				try {
					Thread.sleep(75);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			break;
			
		case THIRD_LEVEL_FREQ:
			
			
			for(i = 0; i < 4; i++){
				
				myWiimote.sendSound();
				
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			
			break;
		/*	
		default:
				
				myWiimote.sendSound();
				
				try {
					Thread.sleep(freqMillis);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				break;*/
		
		}
		
		/*
		
		if(freqMillis == FIFTH_LEVEL_FREQ){
			
			int i;
			
			for(i = 0; i < 3; i++){
				
				myWiimote.sendSound();
				
				try {
					Thread.sleep(75);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
				
				
		}else{
			
			myWiimote.sendSound();
			
			try {
				Thread.sleep(freqMillis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		
		
		}*/
			
			
					
			
			
		
		
		
		
			
		
	}

	/*
	private void uploadSoundFrequency(){		
		int uploaded;
		
		if( (uploaded = super.sharedCell.getFrequencyMillis()) != freqMillis){
			freqMillis = uploaded;			
		}		
		
		
	}*/

	

}
